<template>
	<view>
		<view class="store-banner">
			<view class="title">门店：{{myStore.shop_name}}</view>
			<view class="option">
				<view>余额: {{myStore.balance}}</view>
				<view class="evaluate btn" @click="show = true">添加发货员</view>
				<u-popup v-model="show" mode="center" width="90%" height="350rpx">
					<view class="popup-form">
						<u-form :model="form" ref="uForm">
							<u-form-item label="会员ID" label-position="top"><u-input v-model="form.staff_id" /></u-form-item>
							<u-button type="error" @click="addStaff">确认添加</u-button>
						</u-form>
					</view>
				</u-popup>
			</view>
		</view>
		<u-line></u-line>

		<view class="wrap">
			<view class="u-tabs-box">
				<u-tabs-swiper activeColor="#f29100" ref="tabs" :list="list" :current="current" @change="change"
					:is-scroll="false" swiperWidth="750"></u-tabs-swiper>
			</view>
			<swiper class="swiper-box" :current="swiperCurrent" @transition="transition"
				@animationfinish="animationfinish">
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="order" v-for="(res, index) in staffList" :key="res.id">
								<view class="item">
									<view class="left">
										<image :src="res.avatar" mode="aspectFill"></image>
									</view>
									<view class="content">
										<view class="title u-line-2">{{ res.nickname }}</view>
										<view class="type">{{ res.mobile }}</view>
										<view class="evaluate btn" @click="removeStaff(res.id)">移除</view>
									</view>
								</view>
							</view>
							<view v-if="staffList.length===0">
								<view class="centre">
									<image src="https://cdn.uviewui.com/uview/template/taobao-order.png" mode="">
									</image>
									<view class="explain">
										门店发货员列表为空
										<!-- <view class="tips">可以去看看有那些想买的</view> -->
									</view>
									<!-- <view class="btn" @click="toHome">随便逛逛</view> -->
								</view>
							</view>
							<!-- <u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2"></u-loadmore> -->
						</view>
					</scroll-view>
				</swiper-item>
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="order" v-for="(res, index) in balanceLog" :key="res.id">
								<view class="item">
									<view class="content">
										<view class="store">{{$u.timeFormat(res.created, 'yyyy-mm-dd hh:MM:ss')}}</view>
										<view class="type">{{ res.desc }}</view>
									</view>
									<view class="right">
										<view class="price">￥{{ res.balance_change }}</view>
										<view class="price-after">￥{{ res.balance_after }}</view>
									</view>
								</view>
							</view>
							<view v-if="balanceLog.length===0">
								<view class="centre">
									<image src="https://cdn.uviewui.com/uview/template/taobao-order.png" mode="">
									</image>
									<view class="explain">
										暂无余额记录
										<!-- <view class="tips">可以去看看有那些想买的</view> -->
									</view>
									<!-- <view class="btn" @click="toHome">随便逛逛</view> -->
								</view>
							</view>
							<!-- <u-loadmore :status="loadStatus[1]" bgColor="#f2f2f2"></u-loadmore> -->
						</view>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				show: false,
				form: {
					staff_id: ''
				},
				paging: [{
					current_page: 1,
					total_pages: 1,
					page_size: 50
				}, {
					current_page: 1,
					total_pages: 1,
					page_size: 50
				}],
				list: [
					{
						name: '门店员工'
					},
					{
						name: '账户明细'
					}
				],
				myStore:{},
				staffList:[],
				balanceLog:[],
				current: 0,
				swiperCurrent: 0,
				tabsHeight: 0,
				dx: 0,
				loadStatus: ['loadmore', 'loadmore']
			}
		},
		onLoad() {
			this.getMyStore()
			this.getList(0)
			this.getList(1)
		},
		methods: {
			getMyStore(){
				this.$u.post('/store-manage/get-my-store', {
				}).then(res => {
					this.myStore = res
				}).catch(e => {
				})
			},
			removeStaff(staffId){
				if(confirm('确认移除？')){
					this.$u.post('/store-manage/remove-store-staff', {
						staff_id:staffId
					}).then(res => {
						this.getList(0)
					}).catch(e => {
					})
				}
			},
			addStaff(){
				if(!this.form.staff_id){
					return uni.showToast({
						title: '请填写员工用户ID',
						icon: 'none',
						duration: 3000
					})
				}
				this.$u.post('/store-manage/add-store-staff', {
					staff_id:this.form.staff_id
				}).then(res => {
					this.getList(0)
					uni.showToast({
						title: '已添加',
						icon: 'success',
						duration: 3000
					})
					this.show = false
				}).catch(e => {
					uni.showToast({
						title: '添加失败',
						icon: 'error',
						duration: 3000
					})
				})
			},
			getList(idx) {
				if (idx === 0) {
					this.$u.post('/store-manage/get-store-staff-list', {
						page_size: this.paging[idx].page_size,
						current_page: this.paging[idx].current_page
					}).then(res => {
						if (res.page.current_page === 1) {
							this.staffList = res.list;
						} else {
							this.staffList = this.staffList.concat(res.list);
						}
						this.paging[idx].current_page = res.page.current_page;
						this.paging[idx].total_pages = res.page.total_pages;
						this.paging[idx].page_size = res.data.page_size;
					}).catch(e => {
					})
				}else if(idx ===1){
					this.$u.post('/store-manage/get-store-balance-log', {
						page_size: this.paging[idx].page_size,
						current_page: this.paging[idx].current_page
					}).then(res => {
						if (res.page.current_page === 1) {
							this.balanceLog = res.list;
						} else {
							this.balanceLog = this.balanceLog.concat(res.list);
						}
						this.paging[idx].current_page = res.page.current_page;
						this.paging[idx].total_pages = res.page.total_pages;
						this.paging[idx].page_size = res.data.page_size;
					}).catch(e => {
					})
				}
				this.loadStatus.splice(this.current, 1, "loadmore")
			},
			reachBottom() {
				// 此tab为空数据
					this.loadStatus.splice(this.current, 1, "loading")
					setTimeout(() => {
						this.paging[this.current].current_page = ++this.paging[this.current].current_page;
						this.getList(this.current);
					}, 1200);
			},
			// 总件数
			totalNum(item) {
				let num = 0;
				item.map(val => {
					num += val.quantity;
				});
				return num;
			},
			// tab栏切换
			change(index) {
				this.swiperCurrent = index;
				// this.getOrderList(index);
			},
			transition({
				detail: {
					dx
				}
			}) {
				this.$refs.tabs.setDx(dx);
			},
			animationfinish({
				detail: {
					current
				}
			}) {
				this.$refs.tabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.current = current;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.store-banner{
		padding: 30rpx;
		.title{
			font-weight: bold;
		}
		.btn {
			line-height: 52rpx;
			width: 160rpx;
			border-radius: 26rpx;
			border: 2rpx solid $u-border-color;
			font-size: 26rpx;
			text-align: center;
			color: $u-type-info-dark;
		}
		.evaluate {
			color: $u-type-warning-dark;
			border-color: $u-type-warning-dark;
		}
		.option{
			display:flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
		}
		.popup-form{
			padding: 30rpx;
		}
	}
.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;
			justify-content: space-between;
			padding-top: 20rpx;
			padding-bottom:30rpx;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.right {
				color: $u-type-warning-dark;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 140rpx;
					height: 140rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				width:100%;
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
				.btn {
					line-height: 52rpx;
					width: 160rpx;
					border-radius: 26rpx;
					border: 2rpx solid $u-border-color;
					font-size: 26rpx;
					text-align: center;
					color: $u-type-info-dark;
				}
			}

			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 20rpx;
			padding: 0 10rpx;
			justify-content: flex-end;
			align-items: center;



			.evaluate {
				color: $u-type-warning-dark;
				border-color: $u-type-warning-dark;
			}
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}
	.price-after{
		color: gray;
		font-size: .9em;
	}
</style>
